package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.dto.PtLineGuardExcel;
import com.ruoyi.patrol.service.IPtLineRecordGuardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 巡查线路与安保人员关联Controller
 *
 * @author lkm
 * @date 2022-11-12
 */
@RestController
@RequestMapping("/patrol/line_record_guard")
public class PtLineRecordGuardController extends BaseController {
    @Autowired
    private IPtLineRecordGuardService ptLineRecordGuardService;

    /**
     * 查询巡查线路与安保人员关联列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_guard:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtLineGuard ptLineGuard) {
        startPage();
        List<PtLineGuard> list = ptLineRecordGuardService.selectPtLineGuardList(ptLineGuard);
        return getDataTable(list);
    }

    @Log(title = "安保人员巡查记录导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('patrol:line_record:export')")
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PtLineGuard lineGuard) {
        List<PtLineGuard> list = ptLineRecordGuardService.selectPtLineGuardListExcel(lineGuard);
        List<PtLineGuardExcel> result = new ArrayList<>();
        for (PtLineGuard item : list) {
            PtLineGuardExcel var = new PtLineGuardExcel();
            var.setLineName(item.getLine().getLineName());
            var.setDate(item.getDate());
            var.setSchoolName(item.getLine().getSchool().getName());
            var.setGuardName(item.getGuard() != null ? item.getGuard().getGuardName() : item.getGroup().getGroupName());
            var.setPatrolStatus(item.getPatrolStatus() == 0 ? "未开始巡查" : (item.getPatrolStatus() == 1 ? "正在巡查" : "结束巡查"));
            var.setPatrolResult(item.getPatrolStatus() == 0 ? "/" : item.getPatrolResult() == 0 ? "无异常" : item.getPatrolResult() + "项异常");
            result.add(var);
        }
        ExcelUtil<PtLineGuardExcel> util = new ExcelUtil<>(PtLineGuardExcel.class);
        return util.exportExcel(result, "安保人员巡查数据");
    }
}